#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/wait.h>

int main()
{
    while(1){
        printf("[user@host path]$ ");
        fflush(stdout);//刷新标准输出文件的缓冲区
        char input[1024] = {0};
        //scanf("%s",input);
        //printf("[%s]\n",input);
        fgets(input,1023,stdin);//从标准输入获取一行数据
        input[strlen(input) - 1] = '\0';
        //printf("[%s]\n",input);


        char *argv[32] = {NULL};
        int argc = 0;
        argv[argc] = strtok(input," ");
        while(argv[argc]!=NULL){
            argv[++argc] = strtok(NULL," ");
        }

        if(strcmp(argv[0],"cd") == 0){
            chdir(argv[1]);
            continue;
        }
        pid_t child = fork();
        if(child<0){
            perror("fork error");
            continue;
        }else if(child == 0){
            //让子进程运行指令cheng序--程序替换
            execvp(argv[0],argv);
            perror("execvp error");
            exit(-1);
        }
        wait(NULL);
    }
    return 0;
}